﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;

public partial class Evaluate_CheckItemInputD : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Yipusoft.Account.BLL.User.HasPermission(Context.User.Identity.Name, "320402"))
        {
            Response.Write("<script>alert('没有权限！');history.back(0);</script>");
        }
        if (!IsPostBack)
        {
            ViewState["Action"] = "Add";
            ActionTitle.Text = "考核验收成绩录入";
            lblDepartmentName.Text = Department.GetDepartment(Request.QueryString["DepartmentID"]).FullName;
       
            BindGrid();
        }
    }

    private int GetGroupByLen()
    {
        List<RoleInfo> list = Yipusoft.Account.BLL.User.GetRoleList(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserID);

        //是否是司政后防管理员
        bool Flag = list.Where(o => o.RoleID.Equals("100110")).Count() > 0;
        if (Flag)
        {
            return 4;
        }
        else
        {
            return 6;
        }
    }
    private void InitData()
    {
      
        ItemName.Text = "";
        ItemScore.Text = "";
        Description.Text = "";
    }
    private void BindGrid()
    {
        List<CheckItemInfo> items = CheckItem.GetList("DepartmentID2='" + Request.QueryString["DepartmentID"] + "' and CheckID=" + Request.QueryString["CheckID"]);
        UserGrid.DataSource = items;
        UserGrid.DataBind();
    }
    protected void UserGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }
    }
    protected void UserGrid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        ViewState["Action"] = "Edit";
        ItemID.Value = UserGrid.DataKeys[e.NewSelectedIndex].Value.ToString();
        CheckItemInfo item = CheckItem.GetCheckItem(Convert.ToInt32(ItemID.Value));
        ItemName.Text = item.ItemName;
        ItemScore.Text = item.ItemScore.ToString();
        Description.Text = item.Description;
    }
    protected void UserGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ViewState["Action"] = "Add";
        CheckItem.Delete(Convert.ToInt32(UserGrid.DataKeys[e.RowIndex].Value.ToString()));
        BindGrid();
        Prompt.Text = "提示：删除成功！";
    }
    public string GetDepartmentName(string departmentID)
    {
        return Department.GetDepartment(departmentID).FullName;
    }
    private void Insert()
    {
        CheckItemInfo item = new CheckItemInfo();
        item.CheckID = Convert.ToInt32(Request.QueryString["CheckID"]);
        item.DepartmentID = Request["DepartmentID"];
        item.ItemName = ItemName.Text;
        item.ItemScore = decimal.Parse(ItemScore.Text);
        item.Description = Description.Text;
        item.DepartmentID2 = Request["DepartmentID"];
        CheckItem.Insert(item);
        Prompt.Text = "提示：添加成功！";
    }
    private void Update()
    {
        CheckItemInfo item = CheckItem.GetCheckItem(Convert.ToInt32(ItemID.Value));
        item.DepartmentID = Request["DepartmentID"];
        item.ItemName = ItemName.Text;
        item.ItemScore = decimal.Parse(ItemScore.Text);
        item.Description = Description.Text;
        CheckItem.Update(item);
        ViewState["Action"] = "Add";
        Prompt.Text = "提示：更新成功！";
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (ViewState["Action"].ToString() == "Add")
                Insert();
            else
                Update();
            BindGrid();
            InitData();
        }
        catch (Exception ex)
        {
            Prompt.Text = "错误：" + ex.Message;
        }
    }
    protected void ButtonCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("CheckInputD.aspx");
    }
}
